package com.itheima.controller;

import com.itheima.aop.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptServiceImpl deptService;

    //查询所有部门信息
    @GetMapping
    public Result list(){
        log.info("查询所有部门的信息");

        //调用service层方法
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }

    //根据id删除部门
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("根据id删除部门");
        deptService.deleteById(id);
        return Result.success();
    }

    //新增部门
    @Log
    @PostMapping
    public Result insertDept(@RequestBody Dept dept){
        log.info("新增部门");
        deptService.insertDept(dept);
        return Result.success();
    }

    //根据id查询部门
    @GetMapping("/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        log.info("根据id查询部门");
        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }

    //修改部门信息
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门信息");
        deptService.updateDept(dept);
        return Result.success(dept);
    }
}
